package GJAM.SAMPLE.Calculator;

import GJAM.MVC.*;
import javax.microedition.lcdui.*;

public class CalculatorView extends View {

    private TextField operand1TextField;
    private ChoiceGroup operatorChoiceGroup;
    private TextField operand2TextField;
    private TextField resultTextField;

    public CalculatorView(Model model, Display display) {
        super(model, display, "Calculator");

        this.append(getOperand1TextField());
        this.append(getOperatorChoiceGroup());
        this.append(getOperand2TextField());
        this.append(getResultTextField());
        display.setCurrentItem(operand1TextField);
    }

    public void update() {
        int result = ((CalculatorModel) model).getResult();
        resultTextField.setString(String.valueOf(result));
    }

    public int getOperand1() {
        return Integer.parseInt(operand1TextField.getString());
    }

    public int getOperand2() {
        return Integer.parseInt(operand2TextField.getString());
    }

    public String getOperator() {
        return operatorChoiceGroup.getString(operatorChoiceGroup.getSelectedIndex());
    }

    public ChoiceGroup getOperatorChoiceGroup() {
        if (operatorChoiceGroup == null) {
            operatorChoiceGroup = new ChoiceGroup("", Choice.EXCLUSIVE);
            operatorChoiceGroup.append("*", null);
            operatorChoiceGroup.append("/", null);
            operatorChoiceGroup.append("+", null);
            operatorChoiceGroup.append("-", null);
            operatorChoiceGroup.setSelectedFlags(new boolean[]{false, false, false, false});
            operatorChoiceGroup.setFont(0, null);
            operatorChoiceGroup.setFont(1, null);
        }
        return operatorChoiceGroup;
    }

    private TextField getResultTextField() {
        if (resultTextField == null) {
            resultTextField = new TextField("Result", "", 32, TextField.ANY);
        }
        return resultTextField;
    }

    private TextField getOperand1TextField() {
        if (operand1TextField == null) {
            operand1TextField = new TextField("", null, 32, TextField.NUMERIC);
        }
        return operand1TextField;
    }

    private TextField getOperand2TextField() {
        if (operand2TextField == null) {
            operand2TextField = new TextField("", null, 32, TextField.NUMERIC);
        }
        return operand2TextField;
    }
}

